/**
 * PasswordValidator - 密码验证模块
 * 
 * 职责:
 * 1. 验证新密码是否满足安全要求（长度大于6位等）
 * 2. 验证两次输入的密码是否一致
 * 3. 处理密码修改表单的客户端验证和提交
 */

const PasswordValidator = {
    /**
     * 检查密码强度是否满足最低要求
     * @param {string} password - 要检查的密码
     * @returns {boolean} - 是否符合最低要求
     */
    checkPasswordStrength(password) {
        // 当前只检查长度是否大于6
        return password && password.length >= 6;
    },
    
    /**
     * 验证两次输入的密码是否一致
     * @param {string} password - 新密码
     * @param {string} confirmPassword - 确认密码
     * @returns {boolean} - 是否一致
     */
    checkPasswordMatch(password, confirmPassword) {
        return password === confirmPassword;
    },
    
    /**
     * 验证表单
     * @param {HTMLFormElement} form - 密码修改表单
     * @returns {object} - 验证结果 {valid: boolean, message: string}
     */
    validateForm(form) {
        const oldPassword = form.elements['old_password'].value;
        const newPassword = form.elements['new_password'].value;
        const confirmPassword = form.elements['confirm_password'].value;
        
        // 验证旧密码是否已填写
        if (!oldPassword) {
            return { valid: false, message: '请输入当前密码' };
        }
        
        // 验证新密码是否符合强度要求
        if (!this.checkPasswordStrength(newPassword)) {
            return { valid: false, message: '新密码长度必须大于等于6位' };
        }
        
        // 验证两次输入的密码是否一致
        if (!this.checkPasswordMatch(newPassword, confirmPassword)) {
            return { valid: false, message: '两次输入的新密码不一致' };
        }
        
        // 验证新密码与旧密码不能相同
        if (newPassword === oldPassword) {
            return { valid: false, message: '新密码不能与当前密码相同' };
        }
        
        return { valid: true, message: '验证通过' };
    },
    
    /**
     * 初始化密码修改表单验证
     * @param {string} formId - 表单ID
     */
    initFormValidation(formId = 'passwordChangeForm') {
        const form = document.getElementById(formId);
        if (!form) return;
        
        // 表单提交事件处理
        form.addEventListener('submit', (e) => {
            // 阻止默认提交
            e.preventDefault();
            
            // 验证表单
            const validationResult = this.validateForm(form);
            
            if (!validationResult.valid) {
                // 显示错误消息
                if (window.show_noti) {
                    window.show_noti(validationResult.message, 'error');
                } else {
                    alert(validationResult.message);
                }
                return;
            }
            
            // 验证通过，提交表单
            form.submit();
        });
        
        // 密码输入时实时验证
        const newPasswordInput = form.elements['new_password'];
        const confirmPasswordInput = form.elements['confirm_password'];
        
        if (newPasswordInput) {
            newPasswordInput.addEventListener('input', () => {
                // 密码强度检查
                const password = newPasswordInput.value;
                const strengthValid = this.checkPasswordStrength(password);
                
                // 更新密码强度提示
                const strengthFeedback = document.getElementById('passwordStrengthFeedback');
                if (strengthFeedback) {
                    if (!password) {
                        strengthFeedback.textContent = '';
                        strengthFeedback.className = '';
                    } else if (strengthValid) {
                        strengthFeedback.textContent = '密码强度符合要求';
                        strengthFeedback.className = 'text-success';
                    } else {
                        strengthFeedback.textContent = '密码长度必须大于等于6位';
                        strengthFeedback.className = 'text-danger';
                    }
                }
                
                // 如果确认密码已有输入，检查两次密码是否一致
                if (confirmPasswordInput && confirmPasswordInput.value) {
                    const matchValid = this.checkPasswordMatch(password, confirmPasswordInput.value);
                    const matchFeedback = document.getElementById('passwordMatchFeedback');
                    
                    if (matchFeedback) {
                        if (matchValid) {
                            matchFeedback.textContent = '两次密码输入一致';
                            matchFeedback.className = 'text-success';
                        } else {
                            matchFeedback.textContent = '两次输入的密码不一致';
                            matchFeedback.className = 'text-danger';
                        }
                    }
                }
            });
        }
        
        if (confirmPasswordInput) {
            confirmPasswordInput.addEventListener('input', () => {
                // 两次密码是否一致检查
                const password = newPasswordInput.value;
                const confirmPassword = confirmPasswordInput.value;
                const matchValid = this.checkPasswordMatch(password, confirmPassword);
                
                // 更新密码一致性提示
                const matchFeedback = document.getElementById('passwordMatchFeedback');
                if (matchFeedback) {
                    if (!confirmPassword) {
                        matchFeedback.textContent = '';
                        matchFeedback.className = '';
                    } else if (matchValid) {
                        matchFeedback.textContent = '两次密码输入一致';
                        matchFeedback.className = 'text-success';
                    } else {
                        matchFeedback.textContent = '两次输入的密码不一致';
                        matchFeedback.className = 'text-danger';
                    }
                }
            });
        }
    }
};

// 页面加载完成后初始化密码修改表单验证
document.addEventListener('DOMContentLoaded', () => {
    PasswordValidator.initFormValidation('passwordChangeForm');
});

// 导出为全局对象
window.PasswordValidator = PasswordValidator;
